package com.ftg.learn.bus.exam_record.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.exam_record.entity.ExamRecord;
import com.ftg.learn.bus.exam_record.mapper.ExamRecordMapper;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamRecordService {

    @Autowired
    ExamRecordMapper erm;

    /**
     * 查询
     * @return
     */
    public List<ExamRecord> showAll() {
        QueryWrapper<ExamRecord> qw = new QueryWrapper<>();
        return erm.selectList(qw);
    }

    /**
     * 删除单条
     * @param id
     * @return
     */
    public int delete(String id) {
        if(id == null || id.equals("")){
            return -1;
        }else{
            return erm.deleteById(id);
        }
    }

    /**
     * 查一条记录
     * @param id
     * @return
     */
    public ExamRecord findOne(String id) {
        return erm.selectById(id);
    }

    /**
     * 审批记录
     * @return
     */
    public int ExamUpdateRecord(String id,String name){
        String sid = "审批一条数据,相关车牌号:"+id;
        return erm.autoInsert(sid,name);
    }

    /**
     * 分页查询
     * @param page              总页数
     * @param limit             每页显示条数
     * @param examrec_id          操作id
     * @param examrec_operater    操作人
     * @return
     */
    public ResultType<ExamRecord> query(int page, int limit, String examrec_id, String examrec_operater){

        ResultType<ExamRecord> rt = new ResultType<>();
        rt.setCode(0);

        IPage<ExamRecord> ip = new Page<>(page,limit);

        QueryWrapper<ExamRecord> qw = new QueryWrapper<>();
        qw.and(wrapper -> wrapper.like("examrec_id",examrec_id).like("examrec_operater",examrec_operater));

        ip =  erm.selectPage(ip,qw);
        rt.setData(ip.getRecords());
        rt.setCount(ip.getTotal());
        return rt;
    }
}
